import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
import NotFound from '../views/NotFound'
import UserList from '../views/personnel/UserList'
import Aside from '../views/layout/Aside'
import Header from '../views/layout/Header'
import Body from '../views/layout/Body'
import WorderQuery from "../views/worder/WorderQuery"
import ApplyForWorder from "../views/worder/ApplyForWorder"
import ApWorkQuery from "../views/apwork-order/ApWorkQuery"
import Supervising from "../views/apwork-order/Supervising"
import Information from "../views/e-room/Information"
import Patrol from "../views/e-room/Patrol"
import Leave from "../views/personnel/Leave"
import SparepartsQuery from '../views/spareparts/SparepartsQuery'

Vue.use(Router)





const router = new Router({
  mode: 'history',
  routes: [
    // login路由
    {
      path: '/',
      name: 'login',
      component: Login,
      watch: {
        $route(to, from) {
            window.location.reload(); //监测到路由发生跳转时刷新一次页面
        },
     }
    },

    // 主界面
    {
      path: '/main',
      component: Main,
      props: true,
      redirect: '/worder/worderquery',
      children: [
        // 主页面侧边栏
        {path: '/layout/aside', component: Aside},
        // 顶部导航栏
        {path: '/layout/header', component: Header},
        // el-main 路由展示
        {path: '/layout/body', component: Body},
        // 用户列表 请假 
        {path: '/personnel/userList',
        component: UserList,
        meta: { isAuth: true },
        //人事部才可查看员工信息，进行信息表修改，删除等操作 JSON.parse(localStorage.getItem('user')).department === "人事部"
        beforeEnter: (to, from, next) => {
          if (to.meta.isAuth) { //判断是否需要授权
              if (JSON.parse(localStorage.getItem('user')).department.search("人事部") != -1) {
                  next()  //放行
              } else {
                  alert('抱歉，您无权限查看！')
              }
          } else {
              next()  //放行
          }
        }
      },
        {path: '/personnel/leave',component: Leave},
        // 施工工单查询&申请
        {path: '/worder/worderquery', component: WorderQuery},
        {path: '/worder/applyforworder', component: ApplyForWorder,
        meta: { isAuth: true },
        //判断权限 是否为客户经理
        beforeEnter: (to, from, next) => {
          if (to.meta.isAuth) { //判断是否需要授权
              if (JSON.parse(localStorage.getItem('user')).department.search("客户经理") != -1) {
                  next()  //放行
              } else {
                  alert('抱歉，您无权限申请施工，请联系相关客户经理')
              }
          } else {
              next()  //放行
          }
        }
      },
        // 我的随工，随工查询
        {path: '/apwork-order/apworkquery', component: ApWorkQuery},
        {path: '/apwork-order/supervising', component: Supervising,
        meta: { isAuth: true },
        //现场组才可在我的随工中更新随工工单
        beforeEnter: (to, from, next) => {
          if (to.meta.isAuth) { //判断是否需要授权
              if (JSON.parse(localStorage.getItem('user')).department.search("现场组") != -1) {
                  next()  //放行
              } else {
                  alert('抱歉，您无法签收随工')
              }
          } else {
              next()  //放行
          }
        }},
        // 机房 巡检  机房机柜信息
        {path: '/e-room/information', component: Information},
        {path: '/e-room/patrol', component: Patrol},
        // 备件出/入库 备件查询
        {path: '/spareparts/sparepartsquery', component: SparepartsQuery},
      ]
    },
    //错误页面
    {
      path: '/*',
      component: NotFound
    },
  ],
  
})

router.beforeEach((to, from, next) => {
  if(to.path === '/') {
    return next();
  }
  if(!localStorage.getItem('Authorization')){
      return next('/')
  }
  next();
})
export  default  router